﻿using DataCenter.Model.Interface;
using DataCenter.Model.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataCenter.Provider
{
    public class ResponseSMSProvider : GenericProvider, IResponseSMSProvider
    {

        public List<ResponseSMS> SearchResponseSMS(out int totalRecord, string textSearch, int pagesize = 0, int pageindex = 0)
        {
            var querry = repository.GetQuery<ResponseSMS>().Where(x => x.GabitId == null || string.IsNullOrEmpty(x.GabitId));
            if (!string.IsNullOrWhiteSpace(textSearch))
                querry = querry.Where(x => x.RespType.ToUpper().Contains(textSearch.ToUpper()));

            querry = querry.OrderBy(x => x.RespType);
            totalRecord = querry.Count();
            if (pagesize > 0 && pageindex >= 0)
            {
                querry = querry.Skip(pageindex * pagesize).Take(pagesize);
            }
            return querry.ToList();
        }

        public List<ResponseSMS> SearchResponseSMS(out int totalRecord, string gabitId, string respType, int pagesize = 0, int pageindex = 0)
        {
            var querry = repository.GetQuery<ResponseSMS>().Where(x => x.GabitId == null || string.IsNullOrEmpty(x.GabitId));
            if (!string.IsNullOrWhiteSpace(respType))
                querry = querry.Where(x => x.RespType.ToUpper().Contains(respType.ToUpper()));

            if (!string.IsNullOrWhiteSpace(gabitId))
                querry = querry.Where(x => x.GabitId.ToUpper().Contains(gabitId.ToUpper()));

            querry = querry.OrderBy(x => x.RespType);
            totalRecord = querry.Count();
            if (pagesize > 0 && pageindex >= 0)
            {
                querry = querry.Skip(pageindex * pagesize).Take(pagesize);
            }
            return querry.ToList();
        }

        public ResponseSMS RetrieveResponseSMS(string respType)
        {
            return repository.FindOne<ResponseSMS>(x => x.RespType.ToUpper() == respType);
        }

        public bool Update(ResponseSMS record)
        {
            try
            {
                var updateRecord = RetrieveResponseSMS(record.RespType);
                if (updateRecord == null) return false;
                updateRecord.CreateDate = DateTime.Now;
                updateRecord.GabitTemplate = record.GabitTemplate;
                updateRecord.MsgBodyLen = record.MsgBodyLen;
                updateRecord.Purpose = record.Purpose;
                updateRecord.RespMsg1 = record.RespMsg1;
                updateRecord.RespMsg2 = record.RespMsg2;
                updateRecord.Template = record.Template;
                repository.UnitOfWork.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }


        public bool Add(ResponseSMS obj)
        {
            try
            {
                repository.ResponseSMS.Add(obj);
                repository.UnitOfWork.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
